
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>留言板</title>
    <link href="./Tpl/css/bootstrap.min.css" rel="stylesheet">
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">留言板</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">

          <?php if(isset($_SESSION['username'])){ ?>
              <div class="navbar-form navbar-right">
              <button class="btn btn-primary"><?php echo $_SESSION['username']; ?></button>
              <a href="./loginout.php" class="btn btn-success">退出</a>
            </div>
          <?php }else{ ?>
          <form method="post" action="./login.php" class="navbar-form navbar-right">
            <div class="form-group">
              <input type="text" name="username" placeholder="Email" class="form-control">
            </div>
            <div class="form-group">
              <input type="password" name="password" placeholder="Password" class="form-control">
            </div>
            <button type="submit" class="btn btn-success">登录</button>
            <a href="./reg.php" class="btn btn-success">注册<a/>
          </form>
          <?php } ?>
          
        </div><!--/.navbar-collapse -->
      </div>
    </nav>
  <style type="text/css">
    .info{
      line-height: 40px;
    }
    .glyphicon-ok{
      color: #5CB85C;
    }
    .glyphicon-remove{
      color: red;
    }


  </style>
    <div class="container" style="margin-top:100px;">
      <!-- Example row of columns -->
      <div class="row">
      	<form method="post" action="./adduser.php" class="form-horizontal">
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">用户名</label>
            <div class="col-sm-3">
              <input type="text" class="check-data form-control" data-name="username" id="username" name="username" placeholder="username">
            </div>
            <div class="info col-sm-6">
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label">邮箱</label>
            <div class="col-sm-3">
              <input type="email"  class="check-data form-control" data-name="email" id="mail" name="email" placeholder="Email">
            </div>
            <div class="info col-sm-6">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
            <div class="col-sm-3">
              <input type="password" class="check-data form-control" data-name="password" id="password" name="password" placeholder="Password">
            </div>
            <div class="info col-sm-6">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label">确认密码</label>
            <div class="col-sm-3">
              <input type="password" class="check-data form-control" data-name="repasswrod" id="inputPassword3" name="password" placeholder="Password">
            </div>
            <div class="info col-sm-6">
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-3">
              <button type="submit" class="btn btn-default" id="btn" disabled="">注册</button>
            </div>
          </div>
        </form>
    
      </div>

      <hr>

      <footer>
        <p>&copy; 2016 Company, Inc.</p>
      </footer>
    </div> <!-- /container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>

    <script type="text/javascript">
        $(function  () {
          //给用户框绑定事件
          $('.check-data').blur(function(event) {
              //获取用户输入的数据
              text = $(this).val();
              inputname = $(this).attr("data-name");
              that = $(this);

              if(inputname=="repasswrod"){
                if(text==$("#password").val()){
                    that.parent().siblings('.info').html('<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>两次密码一样');

                    isSubmit();
                }else{
                    that.parent().siblings('.info').html('<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>两次密码不一样');
                    that.css('border', '1px solid red');
                    $("#password").css('border', '1px solid red');
                }
                return;
              }
              // 发异步
              $.ajax({
                url: './checkReg.php',//异步请求地址
                type: 'post',//请求方式
                dataType: 'json',//返回数据类型
                data: {name:inputname,value:text},//推送数据
              })
              .done(function(respones) {
                  //成功执行的方法
                 console.log(respones.info);

                 if(respones.error==0){
                    that.parent().siblings('.info').html('<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>'+respones.info);

                    isSubmit();
                    
                 }else{
                    that.parent().siblings('.info').html('<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>'+respones.info);
                    that.css('border', '1px solid red');

                    $('#btn').attr('disabled', '');
                 }
              })
              .fail(function() {
                //失败执行的方法
                alert("通讯失败")
              })
              
          });
          //加聚焦事件 
          $('.check-data').focus(function(event) {
              $(this).css('border', '1px solid #CCCCCC');
              $(this).parent().siblings('.info').html('');
          });

          function isSubmit () {
              if($('.glyphicon-ok').length==4){
                $('#btn').removeAttr('disabled');
              }
          }
        })
    </script>
  </body>
</html>
